package dao.impl;

import dao.CouponDao;
import pojo.Coupon;
import util.BaseDao;
import util.PageBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CouponImpl implements CouponDao {
    PreparedStatement ps = null;
    Connection con = null;
    ResultSet rs = null;
    PageBean pageBean = new PageBean();

    @Override
    public PageBean<Coupon> getCoupon(PageBean<Coupon> pb) {

        List<Coupon> list = new ArrayList<Coupon>();
        try {
            con = BaseDao.getConnection();
            String s = "select * from (select Rownum r,e_coupon.* from e_coupon where rownum<?)where r>?";
            ps = con.prepareStatement(s);
            ps.setInt(1, pb.getPageIndex() * pb.getPageSize());
            ps.setInt(2, pb.getPageSize() * (pb.getPageIndex() - 1));
            rs = ps.executeQuery();
            while (rs.next()) {
                Coupon cp = new Coupon();
                cp.setcID(rs.getInt("c_id"));
                cp.setcImage(rs.getString("c_image"));
                cp.setcDescription(rs.getString("c_description"));
                cp.setcTerm(rs.getInt("c_term"));
                cp.setcVipPrice(rs.getInt("c_vipprice"));
                cp.setcDeadline(rs.getTimestamp("c_deadline"));
                list.add(cp);
            }
            pageBean.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return pageBean;
    }

    /**
     * 优惠券总记录数
     *
     * @return
     */
    @Override
    public int countCoupon() {
        int a = 0;
        try {
            con = BaseDao.getConnection();
            String s = "select count(*) from e_coupon";
            ps = con.prepareStatement(s);
            rs = ps.executeQuery();
            while (rs.next()) {
                a = rs.getInt(1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }

        return a;
    }

    @Override
    public int updateCoupon(Coupon coupon) {
        int a = 0;
        try {
            con = BaseDao.getConnection();
            String s = "update E_COUPON set c_description=?,c_term=?,c_vipprice=?,c_deadline=? where c_id=?";
            ps = con.prepareStatement(s);
            ps.setString(1, coupon.getcDescription());
            ps.setInt(2, coupon.getcTerm());
            ps.setInt(3,coupon.getcTerm());
            ps.setObject(4, coupon.getcDeadline());
            ps.setInt(5, coupon.getcID());
            a = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }

    @Override
    public Coupon searchCoupon(int id) {
        Coupon cp = new Coupon();
        try {
            con = BaseDao.getConnection();
            String s = "select * from E_COUPON where C_ID=?";
            ps = con.prepareStatement(s);
            ps.setInt(1, id);

            rs = ps.executeQuery();
            while (rs.next()) {

                cp.setcID(rs.getInt("c_id"));
                cp.setcImage(rs.getString("c_image"));
                cp.setcDescription(rs.getString("c_description"));
                cp.setcTerm(rs.getInt("c_term"));
                cp.setcVipPrice(rs.getInt("c_vipprice"));
                cp.setcDeadline(rs.getTimestamp("c_deadline"));

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return cp;
    }

    @Override
    public int deleteCoupon(int id) {
        int a = 0;
        try {
            con = BaseDao.getConnection();
            String s = "delete from E_COUPON where C_ID=?";
            ps = con.prepareStatement(s);
            ps.setInt(1,id);
            a = ps.executeUpdate();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }

    @Override
    public int insertCoupon(Coupon cp) {
        int a=0;
        try {
            con = BaseDao.getConnection();
            String s = "insert into E_COUPON(c_id,c_description,c_term,c_vipprice,c_deadline) values(sequence_cid.nextval,?,?,?,?)";
            ps = con.prepareStatement(s);
            ps.setString(1,cp.getcDescription());
            ps.setInt(2,cp.getcTerm());
            ps.setInt(3,cp.getcVipPrice());
            ps.setObject(4,cp.getcDeadline());
            a=ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }
    }



